#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <mysql/mysql.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
using namespace std;

#define BUF_SIZE 550
pid_t send_fork=0;
int p_arr[2],sd=0,send_len=0,recv_len=0;
struct sockaddr_in addr;
char send_buf[BUF_SIZE], recv_buf[BUF_SIZE];

socklen_t addr_len=sizeof(addr),cli_sock_len= sizeof(addr);

void send_task(){
    struct sockaddr_in c;		//用于接收管道指针
    socklen_t c_len=0;
    c_len=sizeof(c);
    read(p_arr[0],(void*)&c,(size_t)c_len);
    printf("拿到客户端socket\n");
    for(;;){
        memset(send_buf,0,sizeof(send_buf));
        cin>>send_buf;
        printf("手动发送：%s\n",send_buf);
        if(0>sendto(sd,&send_buf,strlen(send_buf),0,(struct sockaddr*)&c,c_len)){
            printf("send error\n");
            break;
        }
    }
}

typedef struct _ADDR{
    struct in_addr ip;
    int port;

}ADDR;
#define PORT    5683


int main() {
    struct sockaddr_in cli_sock[2]={0};
    ADDR cli_addr[2]={0};

    pipe(p_arr);
    sd=socket(AF_INET,SOCK_DGRAM,0);
    if(0>sd){
        cout<<"create socket error"<<endl;
        return -1;
    }
    addr.sin_family=AF_INET;
    //  addr.sin_addr.s_addr=inet_addr("127.0.0.1");
    addr.sin_port=htons(PORT);

    if(0>bind(sd,(sockaddr*)&addr,sizeof(addr))) {
        printf("bind error\r\n");
        return -1;
    }
    //send_len=sprintf(send_buf,"%s","Hello Client I'm Server : ");
    int t=0,s=0;

#if 0
    send_fork=fork();
    if(send_fork==0){
        send_task();

    }
#endif
    for(;;){
        memset(recv_buf,0,BUF_SIZE);
        if(0>(recv_len=recvfrom(sd,&recv_buf,BUF_SIZE,0,(struct sockaddr*)&cli_sock[t],&cli_sock_len))){
            break;
        }
        printf("client: %s",recv_buf);
        memcpy(&cli_addr[t].ip,&cli_sock[t].sin_addr,sizeof(struct in_addr));
        cli_addr[t].port=cli_sock[t].sin_port;
        printf("client ip: %s, port: %d\n",inet_ntoa(cli_addr[t].ip),ntohs(cli_addr[t].port));
        ++t;
        if(t>=2){
            //cli_sock[1].sin_port=htons(9996);
            sendto(sd,&cli_addr[1],sizeof(ADDR),0,(struct sockaddr*)&cli_sock[0],sizeof(cli_sock[0]));
            sendto(sd,&cli_addr[0],sizeof(ADDR),0,(struct sockaddr*)&cli_sock[0],sizeof(cli_sock[0]));
            sleep(1);
            //cli_sock[0].sin_port=htons(9997);
            sendto(sd,&cli_addr[0],sizeof(ADDR),0,(struct sockaddr*)&cli_sock[1],sizeof(cli_sock[1]));
            sendto(sd,&cli_addr[1],sizeof(ADDR),0,(struct sockaddr*)&cli_sock[1],sizeof(cli_sock[1]));

            close(sd);
            t=0;
            memset(cli_addr,0, sizeof(cli_addr));
            memset(cli_sock,0, sizeof(cli_sock));
            printf("overlap transmit addr ok!\n");
        }


#if 0
        write(p_arr[1],(void*)&cli,(size_t)cli_len);

        for(int i=0;i<recv_len;i++){
            printf("%02x",recv_buf[i]);
        }
        printf("\n---->\r\n");


        t++;
        send_len=sprintf(&send_buf[0],"I receiveid %d data your sent: <%s>\n",t,recv_buf);
        sendto(sd,&send_buf,send_len,0,(struct sockaddr*)&cli,cli_len);
        if(0>recv_len)
            break;
#endif
    }
    close(sd);
    cout << "disconnect" <<endl;
    return 0;
}